const PERSISTED_STORES = new Set(['user', 'property', 'play'])

export function persistPlugin({ store }) {
  if (!PERSISTED_STORES.has(store.$id)) return

  const storageKey = `pinia:${store.$id}`

  try {
    const raw = localStorage.getItem(storageKey)
    if (raw) {
      const parsed = JSON.parse(raw)
      if (parsed && typeof parsed === 'object') {
        store.$patch(parsed)
      }
    }
  } catch (_) {}

  store.$subscribe((_, state) => {
    try {
      localStorage.setItem(storageKey, JSON.stringify(state))
    } catch (_) {}
  }, { detached: true })
}


